from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import mixins, viewsets, filters, authentication, permissions
from rest_framework.pagination import PageNumberPagination
from rest_framework_jwt.authentication import JSONWebTokenAuthentication

from city.models import China
from motion.filters import InvolverMotionPathFilter
from motion.models import InvolverMotionPath
from motion.serializer import InvolverMotionPathSerializer


class InvolverMotionPathPagination(PageNumberPagination):
    page_size = 12
    page_size_query_param = 'pageSize'
    page_query_param = "pageIndex"
    max_page_size = 100


class InvolverMotionPathViewSet(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin,
                              mixins.DestroyModelMixin, viewsets.GenericViewSet, mixins.UpdateModelMixin):
    # throttle_classes = (UserRateThrottle, )
    queryset = InvolverMotionPath.objects.filter(delete_flag=False)  # 数据源
    serializer_class = InvolverMotionPathSerializer
    pagination_class = InvolverMotionPathPagination  # 分页
    authentication_classes = (JSONWebTokenAuthentication, authentication.SessionAuthentication)
    # permission_classes = (permissions.IsAuthenticated, )

    def get_permissions(self):
        return []
        if self.action == "create":

            return []
        if self.request.method != 'OPTIONS':
            pass
            return [permissions.IsAuthenticated()]
        return []

    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = InvolverMotionPathFilter
    # search_fields = ('name', 'mobile')
    ordering_fields = ('id', )

    def perform_destroy(self, instance):
        instance.delete_flag = True
        instance.save()









